package com.qfedu.mtl.controller;

import com.alibaba.fastjson.JSON;
import com.qfedu.mtl.entity.Brand;
import com.qfedu.mtl.query.BrandQuery;
import com.qfedu.mtl.service.BrandService;
import com.qfedu.mtl.service.impl.BrandServiceImpl;
import com.qfedu.mtl.util.CharacterUtil;
import com.qfedu.mtl.util.HttpUtil;
import com.qfedu.mtl.util.StrUtil;
import com.qfedu.mtl.util.page.Pager;
import com.qfedu.mtl.util.web.annotation.Controller;
import com.qfedu.mtl.util.web.annotation.RequestMapping;
import com.qfedu.mtl.vo.TableVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 品牌
 */
@Controller
@RequestMapping("/brand")
public class BrandController {
    //引入业务层组件(对象)
    private BrandService brandService = new BrandServiceImpl();
    //1分页查询功能
    @RequestMapping("/list.do")
    public void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setContentWithJson(req, resp);
        //分页参数
        String page = StrUtil.ifNull(req.getParameter("page"),"1");
        String size = StrUtil.ifNull(req.getParameter("size"),"10");
        //查询条件
        BrandQuery query = HttpUtil.request2Bean(req, BrandQuery.class);
        //调用业务方法分页
        Pager<Brand> pager = brandService.findPage(Integer.valueOf(page), Integer.valueOf(size), query);
        // 前端 table - > {code:xxx  msg:xxx cont:xxx  data:xxx   }
        TableVO vo = TableVO.me().setData(pager.getRows()).setCount(pager.getTotal());
        //把VO转json
        String json = JSON.toJSONString(vo);
        resp.getWriter().write(json);
    }

    //2查询指定品牌
    @RequestMapping("/find.do")
    public void  find(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //设置编码
        CharacterUtil.setContentWithJson(req, resp);
        //获得查询id
        String id = req.getParameter("id");
        //执行查找
        Brand brand = brandService.findById(Integer.valueOf(id));
        //返回数据
        HttpUtil.responseSuccess(brand,resp);
    }

    //3删除指定品牌
    @RequestMapping("/delete.do")
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setContentWithJson(req, resp);
        //获得前端参数
        String id = req.getParameter("id");
        //参数校验
        if( StrUtil.isNullOrEmpty(id)  ){
            HttpUtil.response(null,-1,"请指定删除的ID",resp);
            return;
        }
        //参数校验通过，执行业务
        try{
            brandService.delete( Integer.valueOf(id)  );
            //成功
            HttpUtil.responseSuccess(null,resp);
        }catch (Exception ex){
            //错误
            HttpUtil.response(null,-1, ex.getMessage(),resp);
        }
    }
    //4添加品牌
    @RequestMapping("/add.do")
    public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
            //设置编码
            CharacterUtil.setContentWithJson(req, resp);
            //接收添加数据
            Brand brand = HttpUtil.request2Bean(req, Brand.class);
            brand.setCreateTime(new Date());
            //执行添加
            try{
                brandService.save(brand);
                HttpUtil.responseSuccess(null,resp);
            }catch (Exception e){
                HttpUtil.response(null,-1,e.getMessage(),resp);

            }
    }
    //5修改品牌
    @RequestMapping("/update.do")
    public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //设置编码
        CharacterUtil.setContentWithJson(req, resp);
        //获得修改信息
        Brand brand = HttpUtil.request2Bean(req, Brand.class);
        //执行修改
        try{
            brandService.update(brand);
            HttpUtil.responseSuccess(null,resp);
        }catch (Exception e){
            HttpUtil.response(null,-1,e.getMessage(),resp);
        }
    }

    //6批量处理删除
    @RequestMapping("/delBatch.do")
    public void deleteBatch(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //设置编码
        CharacterUtil.setContentWithJson(req, resp);
        //获得前端批量删除IDS
        String ids = req.getParameter("ids");// ids =  ""
        //如果为null 停止删除
        if(StrUtil.isNullOrEmpty(ids) ){
            HttpUtil.response(null,-1,"请指定删除的id集合",resp);
            return;
        }
        //执行删除
        String[] idss = ids.split(",");
        //转整型数组
        List<Integer> idsss = Stream.of(idss).map(e -> Integer.valueOf(e)).collect(Collectors.toList());
        try{
            brandService.deleteBatch(idsss);
            HttpUtil.responseSuccess(null,resp);
        }catch (Exception ex){
            HttpUtil.response(null,-1,ex.getMessage(),resp);
        }
    }
}
